package com.voice.client;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@Slf4j
@Data
public class Profile {

    public static final boolean ENABLE_SSL = true;
    public static final int DEFAULT_PORT = 443;
    private String endPoint;
    private String host;
    private Integer port;
    private int callbackThreadCorePoolSize = Runtime.getRuntime().availableProcessors();
    private int callbackThreadMaximumPoolSize = Runtime.getRuntime().availableProcessors() * 4;
    private int callbackThreadBlockingQueueSize = 1024;
    private Map<String, String> authParams;

    public String getHost() {
        if(this.host!=null) return host;
        return this.url().map(URL::getHost).orElse(null);
    }

    public int getPort() {
        if(this.port!=null) return port;
        return this.url().map(URL::getPort).filter((p) -> p != -1).orElse(443);
    }

    private Optional<URL> url() {
        try {
            URL url = new URL(this.endPoint);
            return Optional.of(url);
        } catch (MalformedURLException var2) {
            log.error("fail to parse url {},{}", this.endPoint, var2.getMessage());
            return Optional.empty();
        }
    }

    private Profile(String endPoint) {
        this.endPoint = endPoint;
        this.authParams = new HashMap<>();
    }

    public static Profile getAppKeyProfile(String endPoint, String appKey, String appSecret) {
        Profile profile = new Profile(endPoint);
        profile.authParams.put("name", "appKey");
        profile.authParams.put("appKey", appKey);
        profile.authParams.put("appSecret", appSecret);
        return profile;
    }

    public static Profile getDeviceProfile(String endPoint, String mac, String deviceKey,String deviceSecret) {
        Profile profile = new Profile(endPoint);
        profile.authParams.put("name", "deviceKey");
        profile.authParams.put("mac", mac);
        profile.authParams.put("deviceKey", deviceKey);
        profile.authParams.put("deviceSecret", deviceSecret);
        return profile;
    }

}
